﻿using NovoDataBase;
using NovoSerDes.Funcs;
using NovoSerDesEva;
using NovoUI.UserCtrl;
using NovoUSB2XXX;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NovoSerDes.Forms
{
    public partial class RegMap_Form : UIPage
    {
        private  DataSet _ds = null;
        private NovoDataSet _dataSet;
        private SerDesComm novoComm;

        public RegMap_Form()
        {
            novoComm = MainForm.GetSetDesComm;
            if(novoComm != null )
            {
                InitializeComponent();
                if (MainForm.DataSet != null)
                {
                    _dataSet = MainForm.DataSet;
                    int firstIndex = _dataSet.DevInfo.FilePath.LastIndexOf('\\');
                    char[] charsToFind = { 'A', 'B', 'C', 'D','_' };
                    int lastIndex = _dataSet.DevInfo.FilePath.LastIndexOfAny(charsToFind);
                    this.Text = _dataSet.DevInfo.FilePath.Substring(firstIndex + 1, lastIndex - firstIndex);

                    //UITabControl tabCtrlRegView = new UITabControl();
                    //tabCtrlRegView.Dock = DockStyle.Fill;
                    //this.Controls.Add(tabCtrlRegView);

                    //_dataSet = novoDataSet;
                    _ds = _dataSet.DS_Display;
                    tabCtrlRegView.TabPages.Clear();
                    int ix_page = 0;
                    for (int ix_tab = 0; ix_tab < _ds.Tables.Count; ix_tab++)
                    {
                        if ((_ds.Tables[ix_tab].Rows.Count == 0) && (_ds.Tables[ix_tab].TableName != "Customer"))
                            continue;
                        tabCtrlRegView.TabPages.Add(_ds.Tables[ix_tab].TableName);
                        MDRegisterViewTab newTab = new MDRegisterViewTab(_ds.Tables[ix_tab], _ds.Tables["Customer"], _dataSet.RegMap, novoComm);
                        tabCtrlRegView.TabPages[ix_page++].Controls.Add(newTab);
                        newTab.Dock = DockStyle.Fill;
                        newTab.BorderStyle = BorderStyle.Fixed3D;
                    }
                }

                else
                {
                    UIMessageDialog.ShowErrorDialog(FindForm(), "找不到RegMap文件");
                }
            }
            else
            {
               UIMessageDialog.ShowWarningDialog(FindForm(),"未选择CH347通道!!!");
            }
            
           

        }

        public RegMap_Form(string name)
        {
            novoComm = InitSer(name);            
            if (novoComm != null)
            {
                InitializeComponent();
                LogControl log = new LogControl();
                log.Dock = DockStyle.Fill;
                this.uiSplitContainer1.Panel2.Controls.Add(log);
                if (MainForm.DataSet != null)
                {
                    _dataSet = MainForm.DataSet;
                    int firstIndex = _dataSet.DevInfo.FilePath.LastIndexOf('\\');
                    char[] charsToFind = { 'A', 'B', 'C', 'D', '_' };
                    int lastIndex = _dataSet.DevInfo.FilePath.LastIndexOfAny(charsToFind);
                    this.Text = _dataSet.DevInfo.FilePath.Substring(firstIndex + 1, lastIndex - firstIndex);

                    //UITabControl tabCtrlRegView = new UITabControl();
                    //tabCtrlRegView.Dock = DockStyle.Fill;
                    //this.Controls.Add(tabCtrlRegView);

                    //_dataSet = novoDataSet;
                    _ds = _dataSet.DS_Display;
                    tabCtrlRegView.TabPages.Clear();
                    int ix_page = 0;
                    for (int ix_tab = 0; ix_tab < _ds.Tables.Count; ix_tab++)
                    {
                        if ((_ds.Tables[ix_tab].Rows.Count == 0) && (_ds.Tables[ix_tab].TableName != "Customer"))
                            continue;
                        tabCtrlRegView.TabPages.Add(_ds.Tables[ix_tab].TableName);
                        MDRegisterViewTab newTab = new MDRegisterViewTab(_ds.Tables[ix_tab], _ds.Tables["Customer"], _dataSet.RegMap, novoComm);
                        tabCtrlRegView.TabPages[ix_page++].Controls.Add(newTab);
                        newTab.Dock = DockStyle.Fill;
                        newTab.BorderStyle = BorderStyle.Fixed3D;
                    }
                }

                else
                {
                    UIMessageDialog.ShowErrorDialog(FindForm(), "找不到RegMap文件");
                }
            }
            else
            {
                UIMessageDialog.ShowWarningDialog(FindForm(), "未选择CH347通道!!!");
            }
        }
        public RegMap_Form(string name,USBDeviceMeditor usbDeviceMeditor)
        {
            novoComm = InitSer(name);
            if (novoComm == null)
            {
                novoComm = new SerDesComm();
                novoComm.OpenDevice(0);
            }
            if (novoComm != null)
            {
                InitializeComponent();
                LogControl log = new LogControl();
                log.Dock = DockStyle.Fill;
                this.uiSplitContainer1.Panel2.Controls.Add(log);
                if (MainForm.DataSet != null)
                {
                    _dataSet = MainForm.DataSet;
                    int firstIndex = _dataSet.DevInfo.FilePath.LastIndexOf('\\');
                    char[] charsToFind = { 'A', 'B', 'C', 'D', '_' };
                    int lastIndex = _dataSet.DevInfo.FilePath.LastIndexOfAny(charsToFind);
                    this.Text = _dataSet.DevInfo.FilePath.Substring(firstIndex + 1, lastIndex - firstIndex);

                    //UITabControl tabCtrlRegView = new UITabControl();
                    //tabCtrlRegView.Dock = DockStyle.Fill;
                    //this.Controls.Add(tabCtrlRegView);

                    //_dataSet = novoDataSet;
                    _ds = _dataSet.DS_Display;
                    tabCtrlRegView.TabPages.Clear();
                    int ix_page = 0;
                    for (int ix_tab = 0; ix_tab < _ds.Tables.Count; ix_tab++)
                    {
                        if ((_ds.Tables[ix_tab].Rows.Count == 0) && (_ds.Tables[ix_tab].TableName != "Customer"))
                            continue;
                        tabCtrlRegView.TabPages.Add(_ds.Tables[ix_tab].TableName);
                        MDRegisterViewTab newTab = new MDRegisterViewTab(_ds.Tables[ix_tab], _ds.Tables["Customer"], _dataSet.RegMap, novoComm);
                        newTab.SerDesChange(usbDeviceMeditor);
                        tabCtrlRegView.TabPages[ix_page++].Controls.Add(newTab);
                        newTab.Dock = DockStyle.Fill;
                        newTab.BorderStyle = BorderStyle.Fixed3D;
                    }
                }

                else
                {
                    UIMessageDialog.ShowErrorDialog(FindForm(), "找不到RegMap文件");
                }
            }
            
        }
        private SerDesComm InitSer(string name)
        {
            if (MainForm.IniSerCommPairs.ContainsKey(name))
            {
                return (MainForm.IniSerCommPairs[name]);
            }
            else
            {
                //UIMessageDialog.ShowErrorDialog(FindForm(), "请选择CH347通道");
                return null;
            }

        }

    }
}
